﻿using System.ComponentModel;
using System.Drawing;
using System.Xml.Serialization;

namespace Common
{
    public class ViewPort : INotifyPropertyChanged
    {
        private Bitmap image;
        private string name, description, host;
        private int port, screenX, sizeX, screenY, sizeY, posX, posY, calX, calY;

        public event PropertyChangedEventHandler PropertyChanged;

        public ViewPort()
        {
            calX = 0;
            calY = 0;
        }

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }

        public string Description
        {
            get { return description; }
            set
            {
                description = value;
                NotifyPropertyChanged("Description");
            }
        }

        public string Host
        {
            get { return host; }
            set
            {
                host = value;
                NotifyPropertyChanged("Host");
            }
        }

        public int Port
        {
            get { return port; }
            set
            {
                port = value;
                NotifyPropertyChanged("Port");
            }
        }

        public int ScreenCaptureX
        {
            get { return screenX; }
            set
            {
                screenX = value;
                NotifyPropertyChanged("ScreenCaptureX");
            }
        }

        public int ScreenCaptureY
        {
            get { return screenY; }
            set
            {
                screenY = value;
                NotifyPropertyChanged("ScreenCaptureY");
            }
        }

        public int SizeX
        {
            get { return sizeX; }
            set
            {
                sizeX = value;
                changeBitmapSize();
                NotifyPropertyChanged("SizeX");
            }
        }

        public int SizeY
        {
            get { return sizeY; }
            set
            {
                sizeY = value;
                changeBitmapSize();
                NotifyPropertyChanged("SizeY");
            }
        }

        public int ScreenPositionX
        {
            get { return posX; }
            set
            {
                posX = value;
                NotifyPropertyChanged("ScreenPositionX");
            }
        }

        public int ScreenPositionY
        {
            get { return posY; }
            set
            {
                posY = value;
                NotifyPropertyChanged("ScreenPositionY");
            }
        }

        [XmlIgnoreAttribute]
        public Bitmap Image
        {
            get { return image; }
            set
            {
                image = value;
                NotifyPropertyChanged("Image");
            }
        }

        private void changeBitmapSize()
        {
            if ((sizeX > 0) & (sizeY > 0))
            {
                image = new Bitmap(sizeX, sizeY);
                NotifyPropertyChanged("Image");
            }
        }

        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        public Bitmap capture()
        {
            using (Graphics g = Graphics.FromImage(image))
            {
                g.CopyFromScreen(screenX, screenY, 0, 0, new Size(sizeX, sizeY));
            }
            NotifyPropertyChanged("Image");
            return image;
        }

        public Bitmap calibrate()
        {
            Pen pen = new Pen(Color.White);
            if (++calX >= sizeX) calX = 0;
            if (++calY >= sizeY) calY = 0;
            using (Graphics g = Graphics.FromImage(image))
            {
                g.Clear(Color.Black);
                //image.SetPixel(calX, calY, Color.White);
                g.DrawLine(pen, new Point(calX, 0), new Point(calX, sizeY));
                g.DrawLine(pen, new Point(0, calY), new Point(sizeX, calY));
            }
            NotifyPropertyChanged("Image");
            return image;
        }
    }
}
